package com.wc.alorithm_blue_bridge_history.littlewhite.Q231227.Q10472;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/12/27 20:15
 * @description
 */
public class Main {
    public static void main(String[] args) {
        HashMap<String, Long> map = new HashMap<>();
        map.put("B", 1L);
        map.put("KB", 1024L);
        map.put("MB", 1024 * 1024L);
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        while (n-- > 0) {
            long X = sc.nextLong();
            String S = sc.next();
            long Y = sc.nextLong();
            System.out.println(X * map.get(S) / Y);
        }
    }
}
